package cn.wolfcode.wolf2w.controller;


import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.interceptor.RequireLogin;
import cn.wolfcode.wolf2w.interceptor.UserParam;
import cn.wolfcode.wolf2w.query.StrategyCatalogQuery;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.IStrategyRedisService;
import cn.wolfcode.wolf2w.service.*;
import cn.wolfcode.wolf2w.util.JsonResult;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyCommentService strategyCommentService;

    @Autowired
    private IStrategyRedisService strategyRedisService;


    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @GetMapping("themes")
    public Object themes(){
        List<StrategyTheme> list = strategyThemeService.list();
        return JsonResult.success(list);
    }

   @GetMapping("detail")
    public Object detail(String id){
        Strategy strategy = strategyService.get(id);
        //阅读数存入
       strategyRedisService.ViewnumIncrease(id,1);

        return JsonResult.success(strategy);
    }

    @GetMapping("query")
    public Object query(StrategyQuery qo){
        Page<Strategy> page =  strategyService.queryByTheme(qo);
        return JsonResult.success(page);
    }


    @RequireLogin
    @PostMapping("addComment")
    public Object commentAdd(StrategyComment strategyComment, @UserParam UserInfo userInfo){
        //  将源对象的属性拷贝到目标对象，前提：属性名词必须一样才可以拷贝
        //将用户属性设置到评论表中
        BeanUtils.copyProperties(userInfo,strategyComment);
        //用户id不能拷贝，id属性不一致
        strategyComment.setUserId(userInfo.getId());
        strategyCommentService.save(strategyComment);
        //设置评论数
         strategyRedisService.setReplynum(strategyComment.getStrategyId(),1);
        return JsonResult.success();
    }

    @GetMapping("comments")
    public Object comments(StrategyCommentQuery qo){
     Page<StrategyComment> page = strategyCommentService.queryByStrategyId(qo);
     return JsonResult.success(page);
    }


    @RequireLogin
    @PostMapping("commentThumb")
    public Object commentThumb(String cid,@UserParam UserInfo userInfo){
        strategyCommentService.commentThumb(cid,userInfo.getId());
        return JsonResult.success();
    }

    //获取vo对象
    @GetMapping("statisVo")
    public Object statisVo(String sid){
        StrategyStatisVO vo = strategyRedisService.getStringStratisVo(sid);
        return JsonResult.success(vo);
    }


    @RequireLogin
    @PostMapping("/favor")
    public Object favor(String sid,@UserParam UserInfo userInfo){
        Boolean ref = strategyRedisService.favor(sid, userInfo.getId());
        return JsonResult.success(ref);
    }



    @RequireLogin
    @PostMapping("strategyThumbup")
    public Object strategyThumbup(String sid,@UserParam UserInfo userInfo){
        Boolean ref = strategyRedisService.strategyThumbup(sid, userInfo.getId());
        return JsonResult.success(ref);
    }


}
